package com.minisoft.workhour;

import com.minisoft.workhour.model.WorkItem;
import com.minisoft.workhour.service.WorkhourService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Scanner;

/**
 * 工时管理应用主程序
 */
@Slf4j
public class WorkhourApplication {

    private static boolean quit = false;
    
    public static void main(String[] args) {
        log.info("=== 工时管理系统启动 ===");
        
        WorkhourService workhourService = new WorkhourService();
        Scanner scanner = new Scanner(System.in);

        try {
            while (true && !quit) {
                printMenu();
                
                System.out.print("请选择操作 (1-4): ");
//                String choice = scanner.nextLine().trim();
                String choice = "2";
                switch (choice) {
                    case "1":
                        parseAndSubmit(workhourService, scanner);
                        break;
                    case "2":
                        parseOnly(workhourService, scanner);
                        break;
                    case "3":
                        showHelp();
                        break;
                    case "4":
                        log.info("程序退出");
                        return;
                    default:
                        System.out.println("无效选择，请重新输入");
                        break;
                }
                
                System.out.println();
            }
        } finally {
            scanner.close();
        }
    }
    
    /**
     * 打印菜单
     */
    private static void printMenu() {
        System.out.println("=== 工时管理系统 ===");
        System.out.println("1. 解析Excel并提交工时");
        System.out.println("2. 仅解析Excel（不提交）");
        System.out.println("3. 查看帮助");
        System.out.println("4. 退出");
        System.out.println("==================");
    }
    
    /**
     * 解析并提交工时
     */
    private static void parseAndSubmit(WorkhourService workhourService, Scanner scanner) {
        System.out.print("请输入Excel文件路径: ");
        String filePath = scanner.nextLine().trim();
        
        if (filePath.isEmpty()) {
            System.out.println("文件路径不能为空");
            return;
        }
        
        System.out.println("正在解析并提交工时...");
        workhourService.processWeeklyReport(filePath);
    }
    
    /**
     * 仅解析Excel
     */
    private static void parseOnly(WorkhourService workhourService, Scanner scanner) {
        System.out.print("请输入Excel文件路径: ");
//        String filePath = scanner.nextLine().trim();
//        String filePath = "D:\\周报\\华发股份外包人员工作周报-陈珏谡 - 工时.xlsx";
        String filePath = "D:\\周报\\华发股份外包人员工作周报-陈珏谡 - 工时 - GF.xlsx";
        if (filePath.isEmpty()) {
            System.out.println("文件路径不能为空");
            return;
        }
        
        System.out.println("正在解析Excel文件...");
        List<WorkItem> workItems = workhourService.parseOnly(filePath);
        
        if (!workItems.isEmpty()) {
            System.out.print("是否要提交这些工时记录？(y/n): ");
            String confirm = scanner.nextLine().trim().toLowerCase();
            
            if ("y".equals(confirm) || "yes".equals(confirm)) {
                System.out.println("正在提交工时记录...");
                int successCount = workhourService.submitWorkItems(workItems);
                System.out.println("提交完成，成功: " + successCount + "/" + workItems.size());
                quit = true;
            }
        }
    }
    
    /**
     * 显示帮助信息
     */
    private static void showHelp() {
        System.out.println("=== 使用说明 ===");
        System.out.println("1. Excel文件格式要求：");
        System.out.println("   - Sheet名称格式：2025.06.02-2025.06.08");
        System.out.println("   - 工作内容在C11-C17单元格");
        System.out.println("   - 工作项格式：序号、工作项--花费工时--任务编号");
        System.out.println("   - 示例：1、珠海大区数字化专班费效分析表需求分析--2小时--2MKV4kwmv6D8reRS");
        System.out.println();
        System.out.println("2. 程序功能：");
        System.out.println("   - 自动解析Excel中的工作项");
        System.out.println("   - 转换为API所需格式");
        System.out.println("   - 调用接口提交工时记录");
        System.out.println();
        System.out.println("3. 注意事项：");
        System.out.println("   - 确保Excel文件格式正确");
        System.out.println("   - 确保网络连接正常");
        System.out.println("   - 提交前可先选择\"仅解析\"查看结果");
        System.out.println("================");
    }
}
